home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / demos / audio / drive / Engine.c++ < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  5.9 KB  |  259 lines

  1. /*
  2.  * Copyright 1992-1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. //////////////////////////////////////////////////////////////////////
  18. // Engine.c++ - implementation of the engine class
  19. // This is really the "drivetrain" class. It contains everything
  20. // relating to how the car moves.
  21. //////////////////////////////////////////////////////////////////////
  22.  
  23. #include "Simulation.h"
  24. #include "Driver.h"
  25. #include "Engine.h"
  26. #include "Cockpit.h"
  27. #include "Dynamics.h"
  28. #include "Road.h"
  29. #include "Car.h"
  30. #include "Noise.h"
  31.  
  32. const int CRANKS_TO_START = 6;
  33.  
  34. Engine::Engine(Car *c)
  35. {
  36.     _car = c;
  37.  
  38.     _yaw_per_steering = 0.01;
  39.     
  40.     _max_speed = 220;
  41.     _max_rpm = 8000;
  42.     _redline = 6500;
  43.     _idle = 850;
  44.     _stall = 400;
  45.  
  46.     _compression = 200; // rpms lost per second
  47.     _rpm_per_gas = 4000; // rpms gained per gas per second
  48.  
  49.     _max_fuel = 13.0;
  50.     _mileage = 30.0;
  51.     _max_oil = 120.0;
  52.     _max_temp = 200.0;
  53.     
  54.     _tire_diam = 2.0;
  55.  
  56.     // Axel revs per Engine rev
  57.     // XXX reverse?
  58.     _ratio[0] = 0.0; 
  59.     _ratio[1] = 0.047;
  60.     _ratio[2] = .10;
  61.     _ratio[3] = .163;
  62.     _ratio[4] = .280;
  63.     _ratio[5] = .373;
  64.  
  65.     // initialize noises to NULL so when can see if they need
  66.     // to be destroyed in the destuctor
  67.     // XXX shouldn't this happen by default?
  68.     _revving_noise = NULL;
  69.     _starting_noise = _cranking_noise = _caf_caf = NULL;
  70.     
  71.     // max time, usecs
  72.     if ((_car->get_type() == LOCAL_CAR) &&
  73.         (_car->get_simulation()->sound_capable()))
  74.     {
  75.         SbString motorpath(_car->get_simulation()->get_path().getString());
  76.         SbString motorfile("motor_loop.aiff");
  77.         motorpath += motorfile;
  78.         
  79.         _revving_noise = new Noise(motorpath.getString(), 
  80.             125000, _car->get_simulation()->play_sound());
  81.         
  82.         SbString startpath(_car->get_simulation()->get_path().getString());
  83.         SbString startfile("start_to_crank.aiff");
  84.         startpath += startfile;
  85.         
  86.         _starting_noise = readsample(startpath.getString());
  87.  
  88.         SbString crankpath(_car->get_simulation()->get_path().getString());
  89.         SbString crankfile("crank_loop.aiff");
  90.         crankpath += crankfile;
  91.         
  92.         _cranking_noise = readsample(crankpath.getString());
  93.  
  94.         SbString cafpath(_car->get_simulation()->get_path().getString());
  95.         SbString caffile("caf_caf.aiff");
  96.         cafpath += caffile;
  97.         
  98.         _caf_caf = readsample(cafpath.getString());
  99.     }
  100.  
  101.     reset_all();
  102. }
  103.  
  104.  
  105. void Engine::reset_all()
  106. {
  107.     stall(FALSE);
  108.     
  109.     _gear = 0;
  110.     _fuel = _max_fuel;
  111.     _oil = 0.7*_max_oil;
  112.     _temp = 0.3*_max_temp;
  113. }
  114.  
  115.  
  116. Engine::~Engine()
  117. {
  118.     // they may not have been read, so check if they exist before deletion
  119.     if (_revving_noise) delete _revving_noise;
  120.     if (_starting_noise) freesample(_starting_noise);
  121.     if (_cranking_noise) freesample(_cranking_noise);
  122.     if (_caf_caf) freesample(_caf_caf);
  123. }
  124.  
  125.  
  126. void Engine::crank(Boolean ignition)
  127. {
  128.     static int num_cranks = 0;
  129.  
  130.     if (_running || (! ignition))
  131.     {
  132.         num_cranks = 0;
  133.         return;
  134.     }
  135.         
  136.     if (_starting_noise && _cranking_noise && 
  137.         _car->get_simulation()->play_sound())
  138.     {
  139.         if (num_cranks == 0)
  140.             playsample(_starting_noise);
  141.  
  142.         playsample(_cranking_noise);
  143.     }
  144.  
  145.     
  146.     if ((++num_cranks >= CRANKS_TO_START) && 
  147.         (_car->get_driver()->get_cockpit()->get_gas() > 0.0))
  148.     {
  149.         num_cranks = 0;
  150.         _running = TRUE;
  151.         if (_revving_noise)
  152.             _revving_noise->setContinuity(TRUE);
  153.     }
  154.  
  155. }
  156.  
  157.  
  158. /*
  159. RPMs are affected by gas, engine compression and friction, and velocity
  160.  
  161. Velocity is affected by RPMs/Gear, brakes, gravity, and friction (wheel/road
  162. and air).
  163.  
  164. Thus we have a feedback loop. The current RPMs are based on the last
  165. velocity. The current velocity is then based on the current RPMs.
  166. */
  167. void Engine::update()
  168. {
  169.     if (! _running)
  170.         return;
  171.  
  172.     float fps = _car->get_simulation()->get_fps();
  173.         
  174.     // rpms based on velocity only if not in neutral
  175.     if (_gear != 0)
  176.     {
  177.         _rpm = (int)
  178.             (_car->get_dynamics()->get_velocity()/(60.0 * 
  179.             this->get_ratio() * this->get_tire_diam() * 
  180.             (1.0/_car->get_road()->get_distance_factor()) * M_PI));
  181.     }
  182.  
  183. //printf("\nrpm = %d\n",_rpm);
  184.  
  185.     // add gas
  186.     // gas is specified as rpms added per second per gas
  187.     _rpm +=
  188.     (int)((((float)_rpm_per_gas - 750.0*(float)_gear)/fps)*_car->get_driver()->get_cockpit()->get_gas());
  189.  
  190. //printf("rpm = %d\n",_rpm);
  191.  
  192.     // subtract engine compression
  193.     // compression is specified as loss of rpm per second
  194.     _rpm -= (int)((float)_compression*(6.0 - (float)_gear)/fps);
  195.  
  196. //printf("rpm = %d\n",_rpm);
  197.  
  198.     if ( ((_gear == 0) || (_gear == 1)) && (_rpm < _idle))
  199.         _rpm = _idle;
  200.     else if (_rpm < _stall)
  201.         stall(_car->get_simulation()->play_sound());
  202.     else if (_rpm > _max_rpm)
  203.         // XXX Engine blowout
  204.         _rpm = _max_rpm;
  205.  
  206.     if (_revving_noise && _car->get_simulation()->play_sound())
  207.         _revving_noise->play(
  208.             .5 + ((float)_rpm/(float)_idle)/3.0, // just sounds good
  209.             _car->get_driver()->last_frame_time());
  210.  
  211.         
  212. }
  213.  
  214.  
  215. void Engine::stall(Boolean noise)
  216. {
  217.     if (_caf_caf && noise)
  218.         playsample(_caf_caf);
  219.         
  220.     _rpm = 0;
  221.     _running = FALSE;
  222.     if (_revving_noise)
  223.         _revving_noise->setContinuity(FALSE);
  224. }
  225.  
  226.  
  227. // returns TRUE if the engine rpms are changing, false
  228. // if rpms are the same as the last time this was called
  229. Boolean Engine::revving() const
  230. {
  231.     static int last_rpm = _idle;
  232.     Boolean result;
  233.     
  234.     result = (_rpm != last_rpm);
  235.  
  236.     last_rpm = _rpm;
  237.  
  238.     return result;
  239. }
  240.  
  241. void Engine::upshift()
  242. {
  243.     if (++_gear > 5)
  244.         _gear = 5;
  245. }
  246.  
  247. void Engine::downshift()
  248. {
  249.     if (--_gear < 0)
  250.         _gear = 0;
  251. }
  252.  
  253.  
  254. void Engine::set_sound(Boolean b)
  255. {
  256.     if (_revving_noise)
  257.         _revving_noise->setContinuity(b);
  258. }
  259.